<?php
namespace app\admin\controller;

use think\facade\Db;
use think\annotation\route\Group;
use think\annotation\Route;
use think\annotation\route\Middleware;
use app\admin\middleware\CheckAdmin;

/**
 * 中检
 * Class MiddleRecords
 * @package app\admin\controller
 * @Group("middle_records")
 *
 */
class MiddleRecords extends Base
{
    /**
     * 中检数据
     *
     * @Route("index", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function index()
    {
        $id = input('id', '', 'trim');
        $page = input('page', '', 'trim');
        $pagesize = input('psize', '', 'trim');
        $res = Db::name('middle_records')->alias('a')
            ->join('project_records b','a.project_record_id = b.id')
            ->where('a.project_record_id',$id)
            ->order('a.id desc')
            ->page($page, $pagesize)
            ->select()->all();
        foreach ($res as $k=>$v) {
            $plan_cate = Db::name('plan_cate')->where('id', $v['plan_cate'])->find();
            $res[$k]['plan_cate'] = $plan_cate ? $plan_cate['name'] : "";
        }
        $result['total'] = Db::name('middle_records')->where('project_record_id',$id)->count();
        $result['data'] = $res;
        return json_ok($result);
    }

    /**
     * 中检数据保存
     * @Route("save_middle_records", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function saveMiddleRecords()
    {
       $id = input('id', '', 'trim');
       $annual = input('annual', '', 'trim');
       $submit_time = input('submit_time', '', 'trim');
       $report_name = input('report_name', '', 'trim');
       $report_id = input('report_id', '', 'trim');
       $remark = input('remark', '', 'trim');
       $main_results = input('main_results', '', 'trim');
       $general_results = input('general_results', '', 'trim');
       $patent_application = input('patent_application', '', 'trim');
       $patent_grant = input('patent_grant', '', 'trim');
       $data = [
           'project_record_id' => $id,
            'annual' => $annual,
           'submit_time' => strtotime($submit_time),
           'report_name' => $report_name,
           'report_id' => $report_id,
           'remark' => $remark,
           'main_results' => json_encode($main_results),
           'general_results' => json_encode($general_results),
           'patent_application' => json_encode($patent_application),
           'patent_grant' => json_encode($patent_grant),
       ];
        Db::name('middle_records')->insert($data);
        return json_ok([], 10000, '操作成功');
    }

    /**
     * 中检数据详情
     * @Route("get_middle_records_by_id", method="POST")
     * @middleware({CheckAdmin::class})
     */
    public function getMiddleRecordsById()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('middle_records')
            ->where('id',$id)
            ->find();
        return json_ok($res);
    }
}